if (!base.exams) base.exams = {};

base.exams = {
	list:[],
	init:function(){
		this.handleQuestion();
		this.confirmHandler();
		window.i = [];


		$(".next .btn-next").off().on("click", function(e){
			url = $(this).data("url");
			id = $(".examWrapper").data("id");
			$.ajax({
			    url: url,
			    dataType: 'json',
			    type: 'POST',
			    contentType: 'application/json',
			    data: '{"id":'+id+'}',
			    complete: function(data) {
			    	
			    	if(data.statusText == "OK"){
                        $(".questionWrapper").html(data.responseText);
                    }else{
			    		base.showError("There was a problem loading 'next' question, please try again.", true);
			        }
			    }
			});
		});
	},
	handleQuestion: function(){
		this.questionForm = $(".well .body");
		this.answers = $(this.questionForm).find(".answer");
		this.btnSubmit = $(this.questionForm).find(".submit");

		$(this.btnSubmit).off().on("click", $.proxy(function(e){
			e.preventDefault();
			jsonObject  = "";
			not_answered =  false;

			//create input hidden when page is reloaded and submit current question.
			//pending..
			if($("#not_answered")[0]){
				not_answered = true;
			}

			for (var i = 0; i < $(this.answers).size(); i++) {
				jsonObject  = jsonObject + '{ "id" : "'+ $($(this.answers)[i]).find('input').val() + '", ';
				if(not_answered && i == 0){
					jsonObject  = jsonObject + ' "selected" : '+ true +' }';
				}else{
					jsonObject  = jsonObject + ' "selected" : '+ $($(this.answers)[i]).find('input').prop("checked") +' }';
				}
				if($(this.answers).size() != parseInt(i+1)) jsonObject = jsonObject + ", ";
			};
			if(!not_answered){
				jsonObject = '{"answers":['+jsonObject+'], "exa_id":"'+$(".examWrapper").data("id")+'", "que_id" :"'+$(".question").data("id")+'" }';
			}else{
				jsonObject = '{"answers":['+jsonObject+'], "exa_id":"'+$(".examWrapper").data("id")+'", "que_id" :"'+$(".question").data("id")+'", "not_answered" : true }';
			}
			this.submitAnswer(jsonObject);
			$(this.btnSubmit).addClass("hide");
		}, this));

		$(this.answers).off().on("click", $.proxy(function(){
			$(this.btnSubmit).removeClass("disabled");
			this.handleQuestion();
		}, this));

	},
	submitAnswer : function(jsonObject){
		me = this;
		$.ajax({
		    url: $(this.questionForm).find("form").attr("action"),
		    dataType: 'json',
		    type: 'POST',
		    contentType: 'application/json',
		    data: jsonObject,
		    complete: function(data) {
		    	var responseText = JSON.parse(data.responseText);
		        if (responseText.responseCode === "000") {
		        	var rightAnswers = responseText.responseMessage.split("|");
		        	for (var i = 0; i < rightAnswers.length; i++) {
		        		window.i.push(rightAnswers[i]);
		        	};
		        	me.afterHandler();
		        } else{
		    		base.showError("There was an error.");
		        }
		    }
		});
	},
	afterHandler:function(){
		//find correct
		var check = false, counter = 0;

		$(this.questionForm).find(".answer.active").append("<span class='right selected'>selected</span>");
		
		//check correct answers
		for (var i = 0; i < window.i.length; i++) {
			var a = window.i[i];

			for (var y = 0; y < this.answers.length; y++) {
				if($(this.answers[y]).hasClass("active") && a == parseInt($(this.answers[y]).data("id"))){
					$(this.answers[y]).removeClass("btn-default").addClass("btn-success");
					counter = counter +1;
				}
			};
		};


		if(counter == window.i.length){
			check = true; //correct/s answer asserted...
			$(".after").find(".explanation").addClass("hide");
		}

		if(!check){
			for (var i = 0; i < window.i.length; i++) {
				var a = window.i[i];
				for (var y = 0; y < this.answers.length; y++) {
					if(a == parseInt($(this.answers[y]).data("id"))){
						$(this.answers[y]).removeClass("btn-default").addClass("btn-success");
					}
				};
				 $(this.questionForm).find(".active").removeClass("btn-default").addClass("btn-danger");
			};

		}
		$(".after").slideDown(500);
	},
	confirmHandler:function(){
	  var $confirm = $('#confirmReload');
		$(window).on('beforeunload', function(e){
			//show confirm dialog
			$confirm.modal("show");
			e.keyCode = 0;
			e.returnValue = false;
		});

		$('.btn-ok.confirm-reload').off().on('click', function(e){
		  e.preventDefault();
		  $confirm.modal("hide");

		  //create input hidden not_answered and submit.

		  //refresh page.

		});
	  /*end delete flow*/
	}
};